<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>xeokit Example</title>
    <link href="../css/pageStyle.css" rel="stylesheet"/>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.13.0/js/all.min.js"></script>
</head>
<body>
<input type="checkbox" id="info-button"/>
<label for="info-button" class="info-button"><i class="far fa-3x fa-question-circle"></i></label>
<canvas id="myCanvas"></canvas>
<div class="slideout-sidebar">
    <img class="info-icon" src="../../assets/images/measure_distance_icon.png"/>
    <h1>Measuring Surface Area of Objects</h1>
    <h2>Mouse over objects to measure their surface areas</h2>
    <p>TODO.</p>
    <h3>Components Used</h3>
    <ul>
        <li>
            <a href="../../docs/class/src/viewer/Viewer.js~Viewer.html"
               target="_other">Viewer</a>
        </li>
        <li>
            <a href="../../docs/class/src/plugins/XKTLoaderPlugin/XKTLoaderPlugin.js~XKTLoaderPlugin.html"
               target="_other">XKTLoaderPlugin</a>
        </li>
    </ul>
    <h3>Assets</h3>
    <ul>
        <li>
            <a href="http://openifcmodel.cs.auckland.ac.nz/Model/Details/274"
               target="_other">Model source</a>
        </li>
    </ul>
</div>
</body>
<script type="module">

    //------------------------------------------------------------------------------------------------------------------
    // Import the modules we need for this example
    //------------------------------------------------------------------------------------------------------------------

    import {Viewer, XKTLoaderPlugin} from "../../dist/xeokit-sdk.min.es.js";

    //------------------------------------------------------------------------------------------------------------------
    // Create a Viewer and arrange the camera
    //------------------------------------------------------------------------------------------------------------------

    const viewer = new Viewer({
        canvasId: "myCanvas",
        readableGeometryEnabled: true
    });

    viewer.camera.eye = [-3.93, 2.85, 27.01];
    viewer.camera.look = [4.40, 3.72, 8.89];
    viewer.camera.up = [-0.01, 0.99, 0.039];

    viewer.cameraControl.followPointer = true;

    //------------------------------------------------------------------------------------------------------------------
    // Load a model
    //------------------------------------------------------------------------------------------------------------------

    const xktLoader = new XKTLoaderPlugin(viewer);

    const sceneModel = xktLoader.load({
        id: "myModel",
        src: "../../assets/models/xkt/v10/glTF-Embedded/Duplex_A_20110505.glTFEmbedded.xkt", // Creates a MetaObject instances in scene.metaScene.metaObjects
        edges: true
    });

    sceneModel.on("loaded", () => {
        viewer.cameraFlight.jumpTo(sceneModel);

        const canvas = window.document.getElementById("myCanvas");
        canvas.addEventListener("mousemove", (function() {
            const px = v => v + "px";

            const metricsDiv = document.createElement("div");
            document.body.appendChild(metricsDiv);
            metricsDiv.style.position = "absolute";
            metricsDiv.style.background = "white";
            metricsDiv.style.borderRadius = "5px";
            metricsDiv.style.padding = "5px 10px";
            metricsDiv.style.pointerEvents = "none";

            let highlighted = null;

            return function(event) {
                const rect = canvas.getBoundingClientRect();
                const canvasPos = [ event.clientX - rect.left, event.clientY - rect.top ];
                const pickRecord = viewer.scene.pick({ canvasPos: canvasPos });
                const pickEntity = pickRecord && pickRecord.entity && pickRecord.entity.model && pickRecord.entity;

                if (highlighted && (highlighted !== pickEntity))
                {
                    highlighted.highlighted = false;
                    highlighted = null;
                }

                if (pickEntity)
                {
                    if (! highlighted)
                    {
                        highlighted = pickEntity;
                        highlighted.highlighted = true;

                        try {
                            metricsDiv.innerHTML = `pickEntity.id = ${pickEntity.id}<br />pickEntity.surfaceArea = ${highlighted.surfaceArea.toFixed(3)} [u²]`;
                        } catch (e) {
                            metricsDiv.innerHTML = e.toString();
                            console.error(e);
                        }
                    }

                    metricsDiv.style.left = px(canvasPos[0] + 20);
                    metricsDiv.style.top  = px(canvasPos[1] - 100);
                }

                metricsDiv.style.display = highlighted ? "" : "none";
            }
        })());
    });

</script>
</html>
